package com.erp.erp_servers.Cost.imp;

import com.erp.erp_dao.Cost.CostDetailDao;
import com.erp.erp_entitys.Cost.entity.CostDetailEntity;
import com.erp.erp_entitys.Cost.req.InsertPlanInStoreProductInfoReq;
import com.erp.erp_entitys.Cost.req.QueryCostDetailWarehouseReq;
import com.erp.erp_entitys.Cost.vo.CostDetailAndPlanInStoreProductInfo;
import com.erp.erp_entitys.Cost.vo.QueryCostDetailWarehouseVO;
import com.erp.erp_entitys.Cost.vo.QueryPlanInStoreProductInfoVo;
import com.erp.erp_servers.Cost.CostDetailService;
import com.erp.utils.DateUtils;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author zjk
 * @version 1.0
 * @description  费用明细实现
 * @date 2020/11/26/026 1:24
 */
@Service
public class CostDetailServiceImpl implements CostDetailService {

    private final CostDetailDao costDetailDao;

    @Autowired
    public CostDetailServiceImpl(CostDetailDao costDetailDao) {
        this.costDetailDao = costDetailDao;
    }

    @Override
    public List<CostDetailEntity> findAllByCode(String costCode) {
        return costDetailDao.findAllByCode(costCode);
    }

    @Override
    public Boolean updateByCostDetailEntity(CostDetailEntity costDetailEntity) {
        return costDetailDao.updateByCostDetailEntity(costDetailEntity);
    }

    @Override
    public int insertSelective(CostDetailEntity record) {
        return costDetailDao.insertSelective(record);
    }

    @Override
    public int insertCostDetailList(List<CostDetailEntity> costDetailList) {
        return costDetailDao.insertCostDetailList(costDetailList);
    }

    @Override
    public int deleteByCostCode(String costCode) {
        return costDetailDao.deleteByCostCode(costCode);
    }

    @Override
    public Integer updateCostDetaiInvalid(Integer valid, List<Integer> ids) {
        return costDetailDao.updateCostDetaiInvalid(valid,ids);
    }

    @Override
    public Integer delCostDetaiInvalid(List<Integer> ids) {
        return costDetailDao.delCostDetaiInvalid(ids);
    }

    @Override
    public Integer updateCostDetaiByCostCode(String costCode) {
        return costDetailDao.updateCostDetaiByCostCode(costCode);
    }

    @Override
    public List<QueryCostDetailWarehouseVO> getUnShareCostDetails(QueryCostDetailWarehouseReq req) {
        String costCode=req.getCostCode();
        String[] costCodeArr=costCode.split(",");
        if (costCodeArr.length>1){
            req.setCostCode(null);
            req.setCostCodeArr(costCodeArr);
        }
        List<QueryCostDetailWarehouseVO> costDetailWarehouseVOList=costDetailDao.getUnShareCostDetails(req);
        for (QueryCostDetailWarehouseVO p:costDetailWarehouseVOList) {
            p.setCreateTimeStr(DateUtils.format(p.getCreateTime(),DateUtils.DATE_PATTERN));
            UUID UU=UUID.randomUUID();
            p.setTempId(UU);
        }
        return costDetailWarehouseVOList;
    }

    @Override
    public List<QueryPlanInStoreProductInfoVo> getPlanInStoreInfo(String invoiceCode) {
        List<QueryPlanInStoreProductInfoVo> list=costDetailDao.getPlanInStoreInfo(invoiceCode);
        for (QueryPlanInStoreProductInfoVo p:list){
            p.setTimeStr(DateUtils.format(p.getTime(),DateUtils.DATE_PATTERN));
            UUID UU=UUID.randomUUID();
            p.setTempId(UU);
            p.setShareCostPrice(new BigDecimal(0));
        }
        return list;
    }

    @Override
    public CostDetailEntity findCostDetailById(Integer detailId) {
        return costDetailDao.selectByPrimaryKey(detailId);
    }

    @Override
    public List<CostDetailAndPlanInStoreProductInfo> getCostDetailAndPlanInStoreProductInfo(Integer detailId, String invoiceCode,Integer share) {
        return costDetailDao.getCostDetailAndPlanInStoreProductInfo(detailId,invoiceCode,share);
    }
}
